﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_DataLayer;
using Gem_Tools_Utility;

namespace Gem_Tools_Controller.Gem_Tools
{
    public class CProject_Pharse_Controller
    {
        private void MapProject_Pharse(DataRow p_row, CProject_Pharse p_objDes)
        {
            DataTable p_dt = p_row.Table;

            if (p_dt.Columns.Contains("Auto_ID"))
                p_objDes.Auto_ID = CUtility.Convert_To_Int32(p_row["Auto_ID"]);

            if (p_dt.Columns.Contains("Group_ID"))
                p_objDes.Group_ID = CUtility.Convert_To_Int32(p_row["Group_ID"]);

            if (p_dt.Columns.Contains("Pharse_Template"))
                p_objDes.Pharse_Template = CUtility.Convert_To_String(p_row["Pharse_Template"]);

            if (p_dt.Columns.Contains("Note"))
                p_objDes.Note = CUtility.Convert_To_String(p_row["Note"]);

            if (p_dt.Columns.Contains("Created"))
                p_objDes.Created = CUtility.Convert_To_DateTime(p_row["Created"]);

            if (p_dt.Columns.Contains("Created_By"))
                p_objDes.Created_By = CUtility.Convert_To_String(p_row["Created_By"]);

            if (p_dt.Columns.Contains("Deleted"))
                p_objDes.Deleted = CUtility.Convert_To_Int32(p_row["Deleted"]);

            if (p_dt.Columns.Contains("Last_Updated"))
                p_objDes.Last_Updated = CUtility.Convert_To_DateTime(p_row["Last_Updated"]);

            if (p_dt.Columns.Contains("Last_Updated_By"))
                p_objDes.Last_Updated_By = CUtility.Convert_To_String(p_row["Last_Updated_By"]);

        }

        public IList<CProject_Pharse> List_Project_Pharse()
        {
            IList<CProject_Pharse> v_arrProject_Pharse = new List<CProject_Pharse>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_List_Project_Pharse");

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CProject_Pharse v_objProject_Pharse = new CProject_Pharse();
                    MapProject_Pharse(v_row, v_objProject_Pharse);
                    v_arrProject_Pharse.Add(v_objProject_Pharse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrProject_Pharse;
        }

        public CProject_Pharse Get_Project_Pharse_By_ID(int p_iID)
        {
            CProject_Pharse v_objProject_Pharse = null;
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_Get_Project_Pharse_By_ID", p_iID);

                if (v_dt.Rows.Count > 0)
                {
                    v_objProject_Pharse = new CProject_Pharse();
                    MapProject_Pharse(v_dt.Rows[0], v_objProject_Pharse);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_objProject_Pharse;
        }

        public int Insert_Project_Pharse(CProject_Pharse p_objData)
        {
            int v_iRes = CConst.INT_VALUE_NULL;

            try
            {
                v_iRes = Convert.ToInt32(CSqlHelper.ExecuteScalar(CConfig.g_strGem_Tools_Conn_String, "sp_ins_Project_Pharse",
                            p_objData.Group_ID, p_objData.Pharse_Template, p_objData.Note, p_objData.Last_Updated_By).ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }


            return v_iRes;
        }

        public void Update_Project_Pharse(CProject_Pharse p_objData)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_upd_Project_Pharse",
                            p_objData.Auto_ID, p_objData.Group_ID, p_objData.Pharse_Template, p_objData.Note, p_objData.Last_Updated_By);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete_Project_Pharse(int p_iID, string p_strLast_Updated_User)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_del_Project_Pharse", p_iID, p_strLast_Updated_User);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
